package com.lagou.stage03;

import java.util.*;

public class StudentMapTest {
    public static void main(String[] args) {
        HashMap<Student, StringBuilder> students = new HashMap<>();
        // 1. 添加几个学生信息和对应地址到Map中用于测试
        students.put(new Student("小红", 12), new StringBuilder("朝阳区"));
        students.put(new Student("小明", 13), new StringBuilder("海淀区"));
        students.put(new Student("小花", 10), new StringBuilder("顺义区"));

        // 2. 利用四种方式遍历Map集合中的内容，格式：key::value

        System.out.println(students);
        // 第一种遍历方式：遍历keys，根据key获得value，再打印
        System.out.println("-----------------------------------");
        for (Student student: students.keySet()) {
            System.out.println(student + "::" + students.get(student));
        }

        // 第二种遍历方式：遍历键值对，通过getKey，getValue获得值并打印
        System.out.println("-----------------------------------");
        for (Map.Entry<Student, StringBuilder> entry: students.entrySet()) {
            System.out.println(entry.getKey() + "::" + entry.getValue());
        }

        // 第三种遍历方式：使用迭代器，迭代entrySet，并打印
        System.out.println("-----------------------------------");
        Iterator<Map.Entry<Student, StringBuilder>> iterator = students.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Student,StringBuilder> entry = iterator.next();
            System.out.println(entry.getKey() + "::" + entry.getValue());
        }

        // 第四种遍历方式：使用迭代器，迭代keys，根据key获得value，并打印
        System.out.println("-----------------------------------");
        Iterator<Student> iterator1 = students.keySet().iterator();
        while (iterator1.hasNext()) {
            Student s = iterator1.next();
            System.out.println(s + "::" + students.get(s));
        }
    }
}
